package client.handler;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import qq.message.DataPackage;
import qq.message.DataPackage.MessageType;
import qq.panel.gui.simple.PanelSimpleGUI;
import qq.panel.gui.simple.ResetList;
/**
 * 客户端处理接收到来自服务器的不同消息的类
 * 不同的函数对应不同的功能
 * @author ZHUAN4
 *
 */
public class ReceiveHandler {

	public void handleReceiveList(DataPackage data,PanelSimpleGUI psg){
		
		//判断是删除logout用户还是更新Login
		if(data.getFromName().equals("server")&&(data.getMessageType()==MessageType.LIST)&&data.getMessageData().startsWith("delete"))
		{
			psg.deleteList(data.getMessageData());
		}
		else {
			System.out.println("get a list message"); 
			System.out.println(data.getMessageData());
			String nameString=data.getMessageData();
			String [] names=(nameString.substring(1, data.getMessageData().length()-1)).split(",");
			TreeSet<String> nameSet=new TreeSet<String>();
			for(int i=0;i<names.length;i++){
				nameSet.add(names[i]);
			}
			psg.resetList(nameSet);
		}
			
//		ResetList rlList=new ResetList(psg,nameSet);
//		new Thread(rlList).start();
	}
	
	public void handleReceiveMessage(DataPackage data,PanelSimpleGUI psg){
		System.out.println("get a message message ");
		StringBuilder str=new StringBuilder();
		str.append(data.getFromName()+"  ");
		str.append(new Date(data.getTime()).toString());
		str.append("\n  ");
		str.append(data.getMessageData());
		str.append("\n");
		psg.getChatTa().append(str.toString());
		psg.getChatTa().updateUI();
	}
	
	public void handleReceiveShake(DataPackage data,PanelSimpleGUI psg){
		System.out.println("get a shake message ");
		psg.shake();
		
	}

	public void handleReceiveLogout(DataPackage receive, PanelSimpleGUI psg) {
		System.out.println("get a logout message ");
		// TODO Auto-generated method stub
		System.exit(0);
	}

}
